package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamic;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamicOperate;
import org.itboys.mobile.entity.mongo.dynamic.SundayDynamicProduct;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.dynamic.SundayDynamicOperateService;
import org.itboys.mobile.service.mongo.web.dynamic.SundayDynamicProductService;
import org.itboys.mobile.service.mongo.web.dynamic.SundayDynamicService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_动态_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/dynamic")
public class SundayDynamicController extends BaseController {

    @Autowired
    private SundayDynamicService dynamicService;
    @Autowired
    private SundayDynamicOperateService dynamicOperateService;
    @Autowired
    private SundayDynamicProductService dynamicProductService;
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         Model model){
        return "/sunday/dynamic/index";
    }
    /**
     * 分页查询动态
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="type",required=false)Integer type

    ) {
        try {
            PageResult<SundayDynamic> result=dynamicService.selectDynamic(request,type);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,CommonConstants.ERROMSG);
        }
    }

    /**
     * 动态详情
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="type",required=true)Integer type,
            Model model){
        SundayDynamic dynamic = dynamicService.findOne(id,type);
        model.addAttribute("dynamic", dynamic);
        return "/sunday/dynamic/input";
    }

    /**
     * 保持动态
     * @param request
     * @param response
     * @param dynamic
     * @param productIds
     * @param imageFiles
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayDynamic dynamic,
                      @RequestParam(value="productIds",required=false) List<Long> productIds,
                      @RequestParam(value="imageFiles",required=false) MultipartFile[] imageFiles
    ) {
        try {
            dynamicService.saveDynamic(dynamic,productIds,imageFiles);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/dynamic/save------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 删除动态
     * @param request
     * @param response
     * @param dynamic
     * @param id
     */
    @RequestMapping("/delete")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayDynamic dynamic,
                      @RequestParam(value="id",required=true) Long id
    ) {
        try {
            dynamicService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/dynamic/delete------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 动态评论或点赞
     * @param request
     * @param response
     */
    @RequestMapping("/operate")
    public void operate(HttpServletRequest request,
                               HttpServletResponse response,
                                @RequestParam(value="dynamicId",required=true)Long dynamicId,
                                @RequestParam(value="type",required=true)Integer type,
                                @RequestParam(value="content",required=false)String content
    ) {
        try {
            dynamicOperateService.operate(dynamicId,type,content);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/dynamic/operate------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 分页查询动态评论或点赞
     * @param request
     * @param response
     */
    @RequestMapping("/operate/select")
    public void operate_select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="type",required=true)Integer type,
                       @RequestParam(value="dynamicId",required=true)Long dynamicId

    ) {
        try {
            PageResult<SundayDynamicOperate> result=dynamicOperateService.selectOperate(request,type,dynamicId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,CommonConstants.ERROMSG);
        }
    }
    /**
     * 删除动态评论或点赞
     * @param request
     * @param response
     */
    @RequestMapping("/operate/delete")
    public void operate_delete(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="id",required=true)Long id
    ) {
        try {
            dynamicOperateService.deleteOperate(id);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/dynamic/operate/delete------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 删除动态评论或点赞
     * @param request
     * @param response
     */
    @RequestMapping("/product/delete")
    public void product_delete(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="id",required=true)Long id
    ) {
        try {
            dynamicProductService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/dynamic/product/delete------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
}
